;
; REAL TIME CLOCK DRIVER INSERT FOR THE DALLAS SEMICONDUCTOR
; "SMARTWATCH", DS1213, WHICH SITS UNDER AN EPROM OR RAM
; CHIP AT A MEMORY ADDRESS WHICH YOU MUST SPECIFY AT LABEL "SWBASE"
;
; THIS CODE IS Z-80 CODE, ASSEMBLED WITH 'DB' PSUDO-OPCODES
; SO 'MAC' WON'T BARF. FOR 8080/85, THE DJNZ, JR,JRNZ,JRZ AND BIT
; TEST INSTRUCTIONS WILL HAVE TO BE RE-WRITTEN USING INTEL LOGICAL
; OPERATORS LEST THE 8080/85 BARF
;
; THIS INSERT WAS WRITTEN FOR USE WITH BYE510 AND PBBS 4.0
; YOU MUST SET BCDBIN=YES IN BYE510 FOR THIS INSERT TO
; ASSEMBLE WITH BYE CORRECTLY.
;
; AUTHOR: MICHAEL R. MCCARREY
;         SOFTWARE ENGINEER
;         ISC SYSTEMS CORP.
;         SPOKANE, WA
;         (509) 927-5105 (BUS)
;         (509) 489-9562 (RES)
;
TIME:	CALL	SWSEL		; GET SMARTWATCH ONLINE
	LXI	H,BCDDATA	; SET POINTER TO BCD DATA STOW
	CALL	READCLK		; READ THE SMARTWATCH
	CALL	EXTRACT		; EXTRACT AND CONVERT BCD VALUES
	RET			; RETURN TO CALLER

;
; THIS IS THE ROUTINE WHICH EXTRACTS AND CONVERTS
; RETURNED BCD DATA FROM THE SMARTWATCH INTO SOMETHING
; THAT CAN BE USED BY THE CALLING PROGRAM
;

EXTRACT	EQU	$
;
;========  EXTRACT AND SAVE SECONDS
	LDA	SWSEC		; LOAD AND
	STA	RTCBUF+2	;  SAVE SECONDS
;
;========  EXTRECT AND CONVERT MINUTES
	LDA	SWMIN		; LOAD AND
	STA	RTCBUF+1	;  SAVE MINUTES
	CALL	BCDBIN		; CONVERT BCD MINUTES
	STA	CCMIN		; UPDATE CURRENT MINUTE
;
;========  EXTRACT AND CONVERT HOURS
	LDA	SWHOUR		; LOAD AND
	STA	RTCBUF+0	;  SAVE HOURS
	CALL	BCDBIN		; CONVERT HOURS TO BINARY
	STA	CCHOUR		; UPDATE CURRENT HOUR
;
;========  EXTRACT AND SAVE DATE
	LDA	SWDATE		; GET DATE
	STA	RTCBUF+6	;  SAVE DATE
;
;========  EXTRACT AND SAVE MONTH
	LDA	SWMONTH		; LOAD AND
	STA	RTCBUF+5	;  SAVE MONTH
;
;======== EXTRACT AND SAVE YEAR
	LDA	SWYEAR		; LOAD AND
	STA	RTCBUF+4	;  SAVE YEAR
;
;======== SET THE CORRECT CENTURY
	MVI	A,19H		; AS IN 19XX
	STA	RTCBUF+3	;  SAVE CENTURY

	RET			; BACK TO CALLER

;
; THIS IS THE SMARTWATCH SELECT ROUTINE
;
; REGISTERS ON ENTRY:
; N/A
;
; REGISTERS ON EXIT:
; HL --> DTKEY+8
;
; REGISTERS DESTROYED: AF, HL
;
SWSEL:	LDA	SWBASE+4	; RESET SMARTWATCH COMPARE REG
	LXI	H,DTKEY		; STRING TO WAKE-UP THE SMARTWATCH
	CALL	WRITCLK		; OK SUCKER, TIME GET UP
	RET			; BACK TO CALLER
;
; THIS IS THE WRITE CLOCK ROUTINE.
;
; REGISTERS ON ENRTY:
; HL --> BCDDATA STOW
;
; REGISTERS ON EXIT:
; HL --> BCDDATA+8
;
; REGISTERS DESTROYED: AF, BC
;
WRITCLK:MVI	B,8		; REGISTER COUNT
T040:	PUSH	B		; SAVE COUNT
	MOV	C,M		; GRAB SOME DATA
	MVI	B,8		; BITS/BYTE
T050:	DB	0CBH		; IS THE 1ST
	DB	041H		;  A ZERO?
	DB	28H		; ZERO, SO
	DB	05H		;  GO AROUND
	LDA	SWBASE+1	; MAKE A0=0
	DB	18H		; GO
	DB	03H		;  AROUND
T070:	LDA	SWBASE+0	; MAKE A0=1
TCONT:	DB	0CBH		; SHIFT RIGHT
	DB	039H		; 1 BIT
	DB	10H		;  UNTIL A COMPLETE
	DB	0F0H		;   BYTE IS ASSEMBLED
	INX	H		; ADJUST POINTER
	POP	B		; RECOVER REGISTER COUNT
	DB	10H		; DO IT TO ALL
	DB	0E8H		;  SMARTWATCH REGISTERS
	RET			; BACK TO CALLER
;
; THIS IS THE READ CLOCK ROUTINE.
;
; REGISTERS ON ENRTY:
; HL --> BCDDATA STOW
;
; REGISTERS ON EXIT:
; HL --> BCDDATA+8
;
; REGISTERS DESTROYED: AF, BC
;
READCLK:MVI	B,8		; REGISTER COUNT IN SMARTWATCH
T020:	PUSH	B		; SAVE REG COUNT
	MVI	C,0
	MVI	B,8		; BITS/BYTE COUNT
T030:	LDA	SWBASE+4	; RAISE A2 (READ)
	ANI	1
	ORA	C		; CLEAR CARRY
	MOV	C,A
	DB	0CBH		; SHIFT IN
	DB	09H		;  A BIT
	DB	10H		;   UNTIL A BYTE IS ASSEMBELED IN 'C'
	DB	0F5H		;    USING THE Z-80 'DJNZ' INST.
	MOV	M,C		; STOW THE BYTE
	INX	H		; ADJUST POINTER
	POP	B		; RECOVER REGISTER COUNT
	DB	10H		; DO ALL 8 REGISTERS
	DB	0EBH		;  USING THAT 'DJNZ' AGAIN
	RET			; BACK TO CALLER

;
; THIS IS THE CODE THAT IS WRITTEN TO THE MEMORY ADDRESS
; THE SMARTWATCH OCCOUPIES. THE SMARTWATCH DETECTS THIS
; PATTERN, DE-SELECTS WHATEVER IS PLUGGED INTO IT, PROM,
; RAM OR WHATEVER, AND THE NEXT 64 ACCESSES TO IT WILL
; BE READS OR WRITES TO OR FROM THE REGISTERS. IT IS OKAY
; TO DO OTHER THINGS, ANY WHERE ELSE IN THE ADDRESS AREA
; AS LONG AS THEY DON'T INVOLVE THE AREA OF THE SMARTWATCH.
;
DTKEY:	DB	0C5H,03AH,0A3H,05CH,0C5H,03AH,0A3H,05CH

;
; THIS IS THE BEGINNING MEMORY ADDRESS FOR THE
; SMARTWATCH. YOU MUST CUSTOMIZE IT TO FIT YOUR
; OWN MEMORY MAP.
;
SWBASE	EQU	0D000H		; BASE ADDRESS FOR ADDRESSING

;
; THIS IS THE STOW FOR BCD DATA FROM OR TO THE
; SMARTWATCH. THE DATA MUST BE READ IN ASCENDING
; ADDRESSES ONLY AS THIS IS THE WAY IT IS PRESENTED
; BY THE SMARTWATCH. IT GOES WITHOUT SAYING THAT
; WRITING TO THE SMARTWATCH MUST ALSO BE DONE IN THE
; SAME MANNER AS READING.
;
BCDDATA:DB	0		; 1/10 SECOND
SWSEC:	DB	0		; SECONDS
SWMIN:	DB	0		; MINUTES
SWHOUR:	DB	0		; HOURS
SWDOW:	DB	0		; DAY OF WEEK
SWDATE:	DB	0		; DATE
SWMONTH:DB	0		; MONTH
SWYEAR:	DB	0		; YEAR

	ENDIF			; CLOCK OR RSPEED
;
;		     end of clock routine
